/*
 * Copyright (c) 2017. hongen (hongen@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alm.token.utils;

import android.text.TextUtils;
import android.util.Log;


/**
 * Log工具，类似android.util.Log。
 * tag自动产生，格式: customTagPrefix:className.methodName(L:lineNumber),
 * customTagPrefix为空时只输出：className.methodName(L:lineNumber)。
 * Author: en
 * Date: 17-04-22
 * Time: 上午10:38
 */
public class LogUtil{

    public static String customTagPrefix = "zuoqiLog";
    private static boolean isLog = true;

    private LogUtil() {
    }

    public static void setIsLog(boolean isLog) {
        LogUtil.isLog = isLog;
    }

    private static String generateTag() {
        StackTraceElement caller = new Throwable().getStackTrace()[2];
        String callerClazzName = caller.getClassName();
        callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
        if (callerClazzName.indexOf("$") != -1)
            callerClazzName = callerClazzName.substring(0, callerClazzName.indexOf("$"));
        String tag = "%s(L:%d)";
        tag = String.format(tag, callerClazzName, caller.getLineNumber());
        tag = TextUtils.isEmpty(customTagPrefix) ? tag : customTagPrefix + ":" + tag;
        return tag;
    }


    public static void d(String content) {
        if (!isLog)
            return;
        Log.d(generateTag(), content);
    }

    public static void d(int content) {
        if (!isLog)
            return;
        Log.d(generateTag(), String.valueOf(content));
    }
    public static void d(long content) {
        if (!isLog)
            return;
        Log.d(generateTag(), String.valueOf(content));
    }
    public static void d(boolean content) {
        if (!isLog)
            return;
        Log.d(generateTag(), String.valueOf(content));
    }
    public static void d(Object content) {
        if (!isLog)
            return;
        Log.d(generateTag(), String.valueOf(content));
    }
    public static void d(String content, Throwable tr) {
        if (!isLog)
            return;
        Log.d(generateTag(), content, tr);
    }

    public static void e(String content) {
        if (!isLog)
            return;
        Log.e(generateTag(), content);
    }

    public static void e(int content) {
        if (!isLog)
            return;
        Log.e(generateTag(), String.valueOf(content));
    }
    public static void e(long content) {
        if (!isLog)
            return;
        Log.e(generateTag(), String.valueOf(content));
    }
    public static void e(boolean content) {
        if (!isLog)
            return;
        Log.e(generateTag(), String.valueOf(content));
    }
    public static void e(Object content) {
        if (!isLog)
            return;
        Log.e(generateTag(), String.valueOf(content));
    }

    public static void e(String content, Throwable tr) {
        if (!isLog)
            return;
        Log.e(generateTag(), content, tr);
    }

    public static void i(String content) {
        if (!isLog)
            return;
        Log.i(generateTag(), content);
    }
    public static void i(int content) {
        if (!isLog)
            return;
        Log.i(generateTag(), String.valueOf(content));
    }

    public static void i(long content) {
        if (!isLog)
            return;
        Log.i(generateTag(), String.valueOf(content));
    }

    public static void i(boolean content) {
        if (!isLog)
            return;
        Log.i(generateTag(), String.valueOf(content));
    }

    public static void i(Object content) {
        if (!isLog)
            return;
        Log.i(generateTag(), String.valueOf(content));
    }

    public static void i(String content, Throwable tr) {
        if (!isLog)
            return;
        Log.i(generateTag(), content, tr);
    }

    public static void v(String content) {
        if (!isLog)
            return;
        Log.v(generateTag(), content);
    }

    public static void v(String content, Throwable tr) {
        if (!isLog)
            return;
        Log.v(generateTag(), content, tr);
    }

    public static void w(String content) {
        if (!isLog)
            return;
        Log.w(generateTag(), content);
    }
    public static void w(int content) {
        if (!isLog)
            return;
        Log.w(generateTag(), String.valueOf(content));
    }
    public static void w(long content) {
        if (!isLog)
            return;
        Log.w(generateTag(), String.valueOf(content));
    }
    public static void w(boolean content) {
        if (!isLog)
            return;
        Log.w(generateTag(), String.valueOf(content));
    }
    public static void w(String content, Throwable tr) {
        if (!isLog)
            return;
        Log.w(generateTag(), content, tr);
    }

    public static void w(Throwable tr) {
        if (!isLog)
            return;
        Log.w(generateTag(), tr);
    }


    public static void wtf(String content) {
        if (!isLog)
            return;
        Log.wtf(generateTag(), content);
    }

    public static void wtf(String content, Throwable tr) {
        if (!isLog)
            return;
        Log.wtf(generateTag(), content, tr);
    }

    public static void wtf(Throwable tr) {
        if (!isLog)
            return;
        Log.wtf(generateTag(), tr);
    }

}
